package com.ruyuan.engine.service;
import com.ruyuan.engine.core.RuleEngineProcessDynamic;
import com.ruyuan.engine.pojo.ClientLog;
import com.ruyuan.engine.pojo.RuleComplexParam;
import com.ruyuan.engine.pojo.RuleEventExecuteParam;
import com.ruyuan.engine.utils.RuleCommonProcessUtils;
import org.apache.flink.api.common.state.ListState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 组合规则的条件匹配
 *
 * clientLog
 * 1.【规则】   2.【规则】   3.【规则】
 *
 * */
public class CombinationRuleServiceImpl implements CombinationRuleService {
    protected static final Logger log = LoggerFactory.getLogger(CombinationRuleServiceImpl.class);

    ListState<ClientLog> clientLogListState;

    public CombinationRuleServiceImpl(ListState<ClientLog> listState) {
        this.clientLogListState = listState;
    }

    @Override
    public boolean mathRuleConditionCount(String eventId, RuleComplexParam ruleComplexParam) {
        //获取组合条件
        List<RuleEventExecuteParam> combinationRuleParams = ruleComplexParam.getCombinationRuleParams();

        try {
            //获取用户的历史行为数据
            Iterable<ClientLog> clientLogs = clientLogListState.get();
            //将历史行为数据和规则进行匹配
            matchClientLogAndCombinationRuleParams(clientLogs,combinationRuleParams);

            for(RuleEventExecuteParam ruleEventExecuteParam:combinationRuleParams) {
                if (ruleEventExecuteParam.getCombinationMatchCount() < ruleEventExecuteParam.getThreshold()) {
                    return false;
                }
            }
            //满足所有条件
            return true;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean mathRuleConditionCount(RuleComplexParam ruleComplexParam) {
        return mathRuleConditionCount(null,ruleComplexParam);
    }

    @Override
    public boolean mathRuleConditionCount(String userID, RuleEventExecuteParam ruleEventExecuteParam) {
        try {
            Iterable<ClientLog> clientLogs = clientLogListState.get();
            for(ClientLog clientLog:clientLogs) {
                boolean isMatch = RuleCommonProcessUtils.eventMatchRuleParam(clientLog, ruleEventExecuteParam, true);
                if (isMatch)ruleEventExecuteParam.setCombinationMatchCount(ruleEventExecuteParam.getCombinationMatchCount()+1);
            }
        }catch (Exception e) {
            e.printStackTrace();
            log.error("获取ListState数据异常");
        }

        return ruleEventExecuteParam.getCombinationMatchCount() >= ruleEventExecuteParam.getThreshold();
    }

    @Override
    public boolean mathRuleConditionCount(RuleEventExecuteParam ruleEventExecuteParam) {
        return mathRuleConditionCount(ruleEventExecuteParam);
    }

    /**
     * 规则匹配
     * */
    public void matchClientLogAndCombinationRuleParams(Iterable<ClientLog> clientLogs, List<RuleEventExecuteParam> combinationRuleParams) {
        //循环客户的历史状态数据
        for(ClientLog log:clientLogs) {
            //进行规则匹配
                for(RuleEventExecuteParam ruleEventExecuteParam:combinationRuleParams) {
                    boolean isMatch = RuleCommonProcessUtils.eventMatchRuleParam(log, ruleEventExecuteParam);
                    if (isMatch) {
                        //修改 组合规则满足的次数
                        ruleEventExecuteParam.setCombinationMatchCount(ruleEventExecuteParam.getCombinationMatchCount()+1);
                    }
                }
        }
    }


}
